import java.io.*;
import java.net.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Martin_A_Klient extends Applet implements Runnable, ActionListener{
	TextArea ta = new TextArea();
	TextField tf = new TextField();
	Martin_A_Ruudustik ruudustik = new Martin_A_Ruudustik();
	Socket sc;
	PrintWriter pw;
	BufferedReader br;
	
	public static final long serialVersionUID = 1;
	
	public Martin_A_Klient(){
		setLayout(new BorderLayout());
		Panel p = new Panel (new GridLayout(2, 1));
		p.add(ta);
		p.add(ruudustik);
		add(p, BorderLayout.CENTER);
		add(tf, BorderLayout.SOUTH);
		tf.addActionListener(this);
		try{
			sc = new Socket("greeny.cs.tlu.ee", 3003);
			pw = new PrintWriter(sc.getOutputStream(), true);
			br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
		}catch(Exception e){e.printStackTrace(); }
		new Thread(this).start();
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == tf){
			pw.println(tf.getText());
			tf.setText("");
		}
	}
	
	public void run(){
		try{
			String rida = br.readLine();
			while(rida != null){
				ta.append(rida+"\n");
				rida = br.readLine();
			}
		}catch(Exception e){e.printStackTrace();}
		ta.append("ots");
	}
}